<template>
  <div>
    <div>&nbsp;&nbsp;</div>
    <div>&nbsp;&nbsp;</div>
    <div>
      <div>
        <van-cell-group>
          <van-row>
            <van-col span="18">
              <van-field
                v-model="chat"
                label=""
                placeholder="输入新消息"
                @keyup.enter.native="handleChat"
                @blur.native.capture="handleBlur"
                @focus.native.capture="handleFocus"
              >
                <template #button />
              </van-field>
            </van-col>
            <van-col span="3">
              <van-button
                icon="comment-o"
                size="small"
                @click.native="handleChat"
              />
            </van-col>
            <van-col span="3">
              <van-button
                icon="add-o"
                size="small"
                @click.native="popupVisible = true"
              />
            </van-col>
          </van-row>
        </van-cell-group>

        <van-popup v-model="popupVisible" position="bottom">
          <van-grid>
            <van-grid-item
              icon="description"
              text="问诊单"
              @click.native="handlePlus('consulation')"
            />
            <van-grid-item
              icon="bill-o"
              text="挂号"
              @click.native="handlePlus('register')"
            />
            <van-grid-item
              icon="bill-o"
              text="All挂号"
              @click.native="handlePlus('allRegister')"
            />
            <van-grid-item icon="photo-o" text="文字" />
            <van-grid-item icon="photo-o" text="文字" />
          </van-grid>
        </van-popup>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ChatInput',
  components: {},

  data() {
    return {
      //
      chat: '',
      popupVisible: false
    }
  },
  computed: {
    //
    channel: function() {
      return this.$store.state.appChat.channel
    },
    isEmployee: function() {
      return this.$store.state.user.isEmployee
    }
  },

  watch: {
    //
  },

  async created() {
    //
  },

  mounted() {
    //
  },

  methods: {
    //

    handleFocus() {
      console.log('xxxx, handleFocus')
      this.$store.dispatch('appChat/postNotifyTyping', true)
    },

    handleBlur() {
      console.log('xxxx, handleBlur')
      this.$store.dispatch('appChat/postNotifyTyping', false)
    },

    handleChat() {
      this.$store.dispatch('appChat/postMessage', this.chat)
      this.chat = ''
    },

    async handlePlus(item) {
      // console.log('handlePlus')
      const grid = {
        consulation: this.handleConsulation,
        register: this.handleRegister,
        allRegister: this.handleAllRegister
        // close: this.handleClose
      }
      await grid[item]()
      this.popupVisible = false
    },
    async handleAllRegister() {
      this.$router.push({
        path: '/po/po'
        // query: { id: po.id }
      })
    },

    async handleRegister() {
      console.log('handleRegister', this.isEmployee, this.channel)
      const action = this.isEmployee
        ? 'SaleOrder/register_by_emp'
        : 'SaleOrder/register_by_cust'
      const res = await this.$store.dispatch(action, { channel: this.channel })
      console.log('handleRegister, SO', res.id)
      const so = this.$store.state.SaleOrder.dataDict
      console.log('handleRegister, SO', so)
      if (so.id) {
        // eslint-disable-next-line object-curly-spacing
        this.$router.push({ path: '/so/register', query: { id: so.id } })
      } else {
        console.log('未有挂号.')
      }
    },

    async handleConsulation() {
      if (!this.isEmployee) {
        // 取得 当前的 咨询单
        // console.log('channel,', this.channel, this.me_partner_id)
        const res = await this.$store.dispatch('ResPartner/browse_one', {
          id: this.channel.opp_partner_id
        })

        // console.log('opp_partner,', res.consultation_id)

        // 发消息
        await this.$store.dispatch('ConsultConsultation/message_post', {
          id: res.consultation_id
          // body: '',
          // author_id: this.channel.me_partner_id,
          // channel_ids: [this.channel.id]
        })

        // const res2 = SO.call('message_post', [1], {
        //   //
        //   body: '',
        //   author_id: this.channel.partner_id,
        //   channel_ids: [this.channel.id]
        // })
      }
    }
  }
}
</script>

<style type="text/css"></style>
